package com.lanou.dllo.shise.com.lanou.dllo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.entity.DataBean;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/28.
 */
public class SameCityListAdapter extends RecyclerView.Adapter<SameCityListAdapter.ViewHolder> {
    private List<DataBean.ResultEntity> dataBeans;
    private RequestQueue queue;

    public SameCityListAdapter(Context c) {
        queue = Volley.newRequestQueue(c);

    }

    public void addData(List<DataBean.ResultEntity> datas) {
        dataBeans = datas;
        notifyDataSetChanged();
    }


    @Override
    public SameCityListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_view_same_city, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SameCityListAdapter.ViewHolder holder, int position) {
        if (dataBeans != null && dataBeans.size() > 0) {
            DataBean.ResultEntity d = dataBeans.get(position);
            holder.sameCityDescTextView.setText(d.getUser_name());
            holder.sameCityTitleTextView.setText(d.getFood_name());
            holder.sameCityComment.setText(d.getComment());
            holder.sameCityAddress.setText(d.getAddress());
            if (d.getPicture_url() != null && d.getPicture_url().length() > 0) {
                String picture_url = d.getPicture_url();

                resultBitmap(picture_url, holder.sameCityImageView);

            }
            if (d.getUser_image() != null && d.getUser_image().length() > 0) {
                String user_image = d.getUser_image();

                resultBitmap(user_image, holder.sameCitySwipeLayout);

            }


        }

    }

    private void resultBitmap(String picture_url, final ImageView sameCityImageView) {
        ImageRequest pictureRequest = new ImageRequest(picture_url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                sameCityImageView.setImageBitmap(response);
            }
        }, 720, 1280, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
                return super.getHeaders();
            }
        };
        queue.add(pictureRequest);
    }

    @Override
    public int getItemCount() {
        if (dataBeans != null && dataBeans.size() > 0) {
            return dataBeans.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView sameCityImageView, sameCitySwipeLayout;
        private TextView sameCityTitleTextView, sameCityDescTextView, sameCityComment, sameCityAddress;

        public ViewHolder(View itemView) {
            super(itemView);
            sameCityDescTextView = (TextView) itemView.findViewById(R.id.sameCityDescTextView);
            sameCityTitleTextView = (TextView) itemView.findViewById(R.id.sameCityTitleTextView);
            sameCityImageView = (ImageView) itemView.findViewById(R.id.sameCityImageView);
            sameCitySwipeLayout = (ImageView) itemView.findViewById(R.id.sameCitySwipeLayout);
            sameCityComment = (TextView) itemView.findViewById(R.id.sameCityComment);
            sameCityAddress = (TextView) itemView.findViewById(R.id.sameCityAddress);

        }
    }
}
